use serde::Deserialize;
use url::Url;

#[derive(Debug, Deserialize)]
pub struct ProjectDependency {
    pub url: String,
    pub is_dev: bool,
}

impl ProjectDependency {
    pub fn name(&self) -> Result<String, Box<dyn std::error::Error>> {
        let parsed_url = Url::parse(&self.url)?;

        let path_segments: Vec<_> = parsed_url
            .path_segments()
            .ok_or("URL has no path segments")?
            .collect();
        if let Some(last_segment) = path_segments.last() {
            if let Some(name) = last_segment.strip_suffix(".git") {
                return Ok(name.to_string());
            } else {
                return Ok(last_segment.to_string()); // 如果没有 .git 后缀，直接返回
            }
        }

        Err("Failed to extract project name from URL path".into())
    }
}
